package com.mycompany.biblio.controller;

import com.mycompany.biblio.business.SecurityService;
import com.mycompany.biblio.business.StatusDAO;
import com.mycompany.biblio.business.UserDAO;
import com.mycompany.biblio.model.Status;
import com.mycompany.biblio.model.User;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;
import javax.faces.model.ListDataModel;


@ManagedBean
@SessionScoped
public class UserController
{

    // ======================================
    // =             Attributes             =
    // ======================================

    @EJB
    private UserDAO userEJB;
    
    @EJB
    private StatusDAO statusEJB;
    
    @EJB
    private SecurityService securityService;
            

    private HtmlDataTable dataTable;

    private User user = new User();
    private ListDataModel userList;


    // ======================================
    // =           Public Methods           =
    // ======================================

   
    public User getCurrentUser()
    {
        return securityService.getCurrentUser();
    }
    
    public String doNew() 
    {
        user = new User();
        return "newUser.xhtml";
    }

    public String doCreate()
    {
        if (!user.getPassword().equals(user.getConfirmPassword()))
        {
            addWarnMessage("Can't create new account : ", "passwords not matched");
            user.setPassword(null);
            user.setConfirmPassword(null);
            return null;
        }
        else
        {
            try
            {
                user = userEJB.create(user);            
            }
            catch (Exception ex)
            {
                addWarnMessage("Can't create new account : ", "username has already been taken");
                return null;
            }
            return "actualites.xhtml";
        }
    }
    
    private void addWarnMessage(String summary, String detail) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, summary, detail));
    }
    
    public String doCancel() {
        return "accueil.xhtml";
    }

    public String doEdit() {
        user = (User)userList.getRowData(); // Voici comment on trouve le livre sélectionné
        return "editUser.xhtml";
    }

    public String doSave() {
        user = userEJB.update(user);
        return "listUsers.xhtml";
    }
    // ======================================
    // =          Getters & Setters         =
    // ======================================

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public HtmlDataTable getDataTable() {
        return dataTable;
    }

    public void setDataTable(HtmlDataTable dataTable) {
        this.dataTable = dataTable;
    }

    
}